#include "Equalize.h"
#include "../utils/Logger.h"

Equalize::Equalize() {
}

Equalize::~Equalize() {
}

IplImage* Equalize::apply(const IplImage* in) {
    Log << "Applying equalize histogram effect." << endl;

    IplImage *out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels);
    if (1 == in->nChannels) {
        cvEqualizeHist(in, out);
    }
    else if (3 == in->nChannels) {
        IplImage *c1 = cvCreateImage(cvSize(in->width, in->height), in->depth, 1);
        IplImage *c2 = cvCreateImage(cvSize(in->width, in->height), in->depth, 1);
        IplImage *c3 = cvCreateImage(cvSize(in->width, in->height), in->depth, 1);

        cvSplit(in, c1, c2, c3, NULL);

        cvEqualizeHist(c1, c1);
        cvEqualizeHist(c2, c2);
        cvEqualizeHist(c3, c3);

        cvMerge(c1, c2, c3, NULL, out);

        cvReleaseImage(&c1);
        cvReleaseImage(&c2);
        cvReleaseImage(&c3);
    }
    else {
        Log << "No equalization done. Channels count = " << in->nChannels << endl;
    }
    
    return out;
}
